<template>
	<div class="toplist">
		<div class="title">
			<span>{{ data.name }}</span>
			<span class="lookmore" @click="topList">查看更多</span>
		</div>
		<div class="top-one">
			<img :src="data.pc_image_text" alt="" @click="goDetail(list[0])">
		</div>
		<div class="tags">
			<span :class="active==0?'active':''" @click="setActive(0)">全部</span>
			<span v-for="(items,index) in nameList" :key="index" :class="active==items?'active':''" @click="setActive(items)">{{items}}</span>
		</div>
		<div class="top-other">
			<el-carousel :interval="5000" arrow="always" height="29.5rem" indicator-position="none">
				<el-carousel-item v-for="items in count" :key="items">
					<div v-for="(item, str) in list" v-if="(str-1) >= (items - 1) * 5 && (str-1) < items * 5&&str>0"
						:key="str" class="goods-item" @click="goDetail(item)">
						<div>
							<img :src="item.defaultAlbum" alt="" class="goods-img">
							<div class="goods-info">
								<p class="name">
									{{ item.pName }}
								</p>
								<p class="desc">
									{{ item.cSellingPoint }}
								</p>
								<p class="price">
									
									<span class="new">￥ {{ item.fSalePrice }}</span>
									<span class="old">￥ {{ item.pfMarkPrice }}</span>
								</p>
								
								
							</div>
						</div>
					</div>
				</el-carousel-item>
			</el-carousel>
		</div>
		
	</div>
</template>

<script>
	export default {
		name: "",
		components: {},
		props: {
			data: {
				type: Object,
				// 对象或数组默认值必须从一个工厂函数获取
				default: function() {
					return {};
				},
			},
		},
		data() {
			return {
				list: [],
				count: "",
				coverList: [],
				second: {},
				active:0,
				cateData:[],
				cateList:[],
				nameList:[],
				copy:[]
			};
		},
		mounted() {
			this.$api.home.getClasses().then((resq) => {
			  if (resq.code == 200) {
			    this.cateData = resq.data;
				
				let data = {
					shop_id: ''
				}
				this.$api.home.sales(data).then((res) => {
					if (res.code == 1) {
						this.list = res.data;
						if (res.data.length >= 1) {
							this.count = Math.ceil((res.data.length - 1) / 5);
						} else {
							this.count = 1
						}
						
						this.list.forEach((item,index)=>{
							// if(index>0){}
							this.getClass(item,this.cateData)			 
						})
						this.nameList = [...new Set(this.nameList)]
						
						this.copy = JSON.parse(JSON.stringify(this.list))
				
					}
				});
			  }
			});
			this.$api.home.getTopList().then((res) => {
				if (res.code == 1) {
					this.coverList = res.data;
				}
			});
			
			
			
			
		},
		methods: {
			setActive(item){
				this.active = item
				let arr = []
				this.copy.forEach(dd=>{
					if(item == dd.className||item == 0){
						arr.push(dd)
					}
					
				})
				this.list = arr
				if (this.list.length >= 1) {
					this.count = Math.ceil((this.list.length - 1) / 5);
				} else {
					this.count = 1
				}
			},
			getClass(item,cateData){
				cateData.forEach(str=>{
					if(item.oProductClass_id == str.id){
						// this.cateList.push({
						// 	name:str.cName,
						// 	id:str.id
						// })
						this.$set(item,'className',str.cName)
						this.nameList.push(str.cName)
					}else{
						if(str.lsChildClass.length>0){
							this.getClass(item,str.lsChildClass)
						}
					}
				})
			},
			topList() {
				this.$router.push({
					path: "/top"
				});
			},
			//跳转商品详情
			goDetail(item) {
				console.log('跳转商品详情', item)
				if (item === undefined) {
					return
				} else {
					this.$router.push({
						path: "/fruit/detail",
						query: {
							id: item.pid,
							iShopId: item.shop_id
						}
					})
				}
			},
		},
	};
</script>

<style scoped lang="scss">
	.toplist {
		padding: 0 10rem;
		margin-top: 2.19rem;
		display: flex;
		flex-direction: column;
		.title {
			width: 100%;
			height: 3.44rem;
			display: flex;
			justify-content: space-between;
			align-items: flex-end;
			font-size: 3rem;
			font-family: SourceHanSansCN;
			font-weight: 400;
			color: #333333;
			margin-bottom: 3.94rem;
			.lookmore{
				font-size: 1.13rem;
				font-family: SourceHanSansCN;
				font-weight: 400;
				color: #999999;
				cursor: pointer;
			}
		}
		.top-one {
			width: 100%;
			height: 26.25rem;
			cursor: pointer;

			img {
				width: 100%;
				height: 26.25rem;
			}
		}
		.tags{
			width: 100%;
			// height: 6.19rem;
			padding: 0 1.25rem;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			flex-wrap: wrap;
			
			span{
				margin-top: 0.8rem;
				// width: 8.63rem;
				// height: 2.38rem;
				padding: 0.5rem 1.5rem;
				background: rgb(27, 224, 186,0.1);
				// opacity: 0.1;
				border-radius: 1rem;
				margin-right: 1.25rem;
				font-size: 1.13rem;
				font-family: SourceHanSansCN;
				font-weight: 500;
				color: #333333;
				line-height: 2.25rem;
				display: flex;
				justify-content: center;
				align-items: center;
				cursor: pointer;
				&.active{
					background: #1BE0BA;
					color: #fff;
					opacity: 1;
				}
				
			}
		}

		.top-other {
			// margin-top: 1.25rem;
			margin-bottom: 1.25rem;

			.el-carousel__item {
				display: flex;
				align-items: center;
				justify-content: flex-start;

				.goods-item {
					width: 19rem;
					min-height: 25.75rem;
					background: #fff;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: space-around;
					position: relative;
					margin-right: 1.25rem;
					cursor: pointer;
					text-align: center;

					&:nth-child(5n) {
						margin-right: 0;
					}

					.goods-img {
						width: 15rem;
						height: 15rem;
						object-fit: contain;
					}

					.goods-info {
						display: flex;
						flex-direction: column;
						text-align: center;
						padding: 0 1rem;
						margin-top: 2rem;

						.name {
							font-size: 1.25rem;
							font-family: SourceHanSansCN;
							font-weight: 400;
							color: #000000;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
							width: 18rem;
							line-height: 1.5;
						}
						.desc{
							font-size: 1rem;
							font-family: SourceHanSansCN;
							font-weight: 400;
							color: #333333;
							opacity: 0.5;
							margin-top: 1.13rem;
							margin-bottom: 1.6rem;
							text-align: center;
						}
						
							.price{
								display: flex;
								justify-content: center;
								align-items: center;
								margin-bottom: 2rem;
								.new{
									font-size: 1.25rem;
									font-family: SourceHanSansCN;
									font-weight: 400;
									color: #FF0000;
									margin-right: 0.94rem;
								}
								.old{
									font-size: 1.25rem;
									font-family: SourceHanSansCN;
									font-weight: 400;
									text-decoration: line-through;
									color: #000000;
									opacity: 0.4;
								}
								
							}
						
					}
				}
			}
		}

		.more {
			height: 2.5rem;
			background: rgba(0, 0, 0, 1);
			display: flex;
			justify-content: flex-end;
			align-items: center;
			padding: 0 1.44rem;

			img {
				width: 7.38rem;
				cursor: pointer;
			}
		}
	}
</style>
